package com.yksj.healthtalk.ui.doctorstation.active;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;

/**
 * 
 * 活动列表
 * @author origin
 *
 */
public class ActivesListActivity extends BaseFragmentActivity implements OnClickListener {
	
	PullToRefreshListView mRefreshListView;
	DataBaseAdapter mAdapter;
	String customerId = SmartFoxClient.getLoginUserId();
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.actives_list_layout);
		
		initUI();
		
		initData();
	}
	
	private void initUI(){
		initTitle();
		setTitle("活动参与");
		setBackBtn("返回",this);
		mRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_listview);
		mRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				initData();
			}
		});
		mRefreshListView.setRefreshing();
		mAdapter = new DataBaseAdapter();
		mRefreshListView.setDividerDrawable(getResources().getDrawable(R.drawable.list_divider_bg));
		mRefreshListView.getRefreshableView().setFooterDividersEnabled(true);
		mRefreshListView.getRefreshableView().addFooterView(getLayoutInflater().inflate(R.layout.list_line_layout, null));
		mRefreshListView.setAdapter(mAdapter);
		mRefreshListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				--position;
				JSONObject jsonObject = mAdapter.getItem(position);
				onQueryDetail(jsonObject);
			}
		});
	}
	
	private void initData(){
		RequestParams params = new RequestParams();
		params.put("customerId", customerId);
		HttpRestClient.doHttpFindActivities(params,
				new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONArray response) {
				mAdapter.onDataChange(response);
				super.onSuccess(statusCode, response);
			}
			@Override
			public void onFinish() {
				super.onFinish();
				mRefreshListView.onRefreshComplete();
			}
		});
	}
	
	private void onQueryDetail(final JSONObject json){
		final String activities_id = json.getString("ACTIVITIES_ID");
		RequestParams params = new RequestParams();
		params.put("customerId", customerId);
		params.put("activityId",activities_id);
		/*customerId 客户id
		  activityId 活动id
		 * */
		HttpRestClient.doHttpFindIsGetPrize(params,
				new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				Intent intent = new Intent(ActivesListActivity.this,ActivesProduceDescActivity.class);
				intent.putExtra("activityId", activities_id);
				intent.putExtra("customerId", customerId);
				intent.putExtra("title", json.getString("ACTIVITIES_NAME"));
				intent.putExtra("json", response.toJSONString());
				startActivity(intent);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	class DataBaseAdapter extends BaseAdapter{
		final JSONArray mArray = new JSONArray();
		final SimpleDateFormat mDateFormat1 = new SimpleDateFormat("yyyyMMddHHmm");
		final SimpleDateFormat mDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		public void onDataChange(JSONArray array){
			mArray.clear();
			mArray.addAll(array);
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mArray.size();
		}

		@Override
		public JSONObject getItem(int position) {
			// TODO Auto-generated method stub
			return mArray.getJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final JSONObject object  = getItem(position);
			final String name = object.getString("ACTIVITIES_NAME");
			String time = null;
			try{
				Date date = mDateFormat1.parse(object.getString("PUBLISH_TIME"));
				time = mDateFormat2.format(date);
			}catch(Exception e){
			}
			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.activites_item_layout,null);
				TextView textView = (TextView)convertView.findViewById(R.id.textView1);
				convertView.setTag(R.id.textView1, textView);
				textView = (TextView)convertView.findViewById(R.id.textView2);
				convertView.setTag(R.id.textView2, textView);
//				TextView button = (TextView)convertView.findViewById(R.id.desc);
//				convertView.setTag(R.id.desc, button);
			}
			TextView nameTextView = (TextView)convertView.getTag(R.id.textView1);
			TextView timeTextView = (TextView)convertView.getTag(R.id.textView2);
//			TextView  button = (TextView)convertView.getTag(R.id.desc);
			
			nameTextView.setText(name);
			timeTextView.setText(time);
			
			/*button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					final String activities_id = object.getString("ACTIVITIES_ID");
					Intent intent = new Intent(ActivesListActivity.this,ActivesProduceDescActivity.class);
					intent.putExtra("ACTIVITIES_ID",activities_id);
					intent.putExtra("title",name);
					startActivityForResult(intent, 1);
				}
			});*/
			return convertView;
		}
	}
}
